package com.example.societiesSystem.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.societiesSystem.dto.ActivityLogsAddDTO;
import com.example.societiesSystem.dto.ApplyLogsAddDTO;
import com.example.societiesSystem.entity.*;
import com.example.societiesSystem.entity.ApplyLogs;
import com.example.societiesSystem.mapper.ApplyLogsMapper;
import com.example.societiesSystem.service.IActivitiesService;
import com.example.societiesSystem.service.IApplyLogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.societiesSystem.service.ITeamsService;
import com.example.societiesSystem.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 入团申请信息表 服务实现类
 * </p>
 *
 * @author 周欢
 * @since 2025-05-27
 */
@Service
public class ApplyLogsServiceImpl extends ServiceImpl<ApplyLogsMapper, ApplyLogs> implements IApplyLogsService {

    @Resource
    private IActivitiesService activitiesService;

    @Resource
    private ITeamsService teamsService;

    @Resource
    private IUserService userService;

    @Override
    public IPage<ApplyLogs> pageAll(Long pageIndex, Long pageSize, String token, String teamName, String userName) {
        IPage<ApplyLogs> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<ApplyLogs> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ApplyLogs::getTeamId, token);
        queryWrapper.like(CharSequenceUtil.isNotBlank(teamName), ApplyLogs::getTeamName, teamName);
        queryWrapper.like(CharSequenceUtil.isNotBlank(userName), ApplyLogs::getUserName, userName);
        return this.page(page, queryWrapper);
    }

    @Override
    public Boolean add(ApplyLogsAddDTO applyLogsAddDTO) {
        ApplyLogs applyLogs = new ApplyLogs();
        applyLogs.setTeamId(applyLogsAddDTO.getTeamId());
        applyLogs.setTeamName(applyLogsAddDTO.getTeamName());
        User user = userService.getById(applyLogsAddDTO.getToken());
        applyLogs.setUserId(String.valueOf(user.getId()));
        applyLogs.setUserName(user.getName());
        applyLogs.setUserGender(user.getGender());
        applyLogs.setUserPhone("13203050639");
        applyLogs.setCreateTime(LocalDateTime.now());
        applyLogs.setStatus(applyLogsAddDTO.getStatus());
        Teams teams = teamsService.getById(applyLogsAddDTO.getTeamId());
        applyLogs.setSocietiesId(teams.getTypeId());
        return this.save(applyLogs);
    }
}
